﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ECOSYSTEM_BL;
using ECOSYSTEM_Dominio;

namespace ECOSYSTEMPRESENTACION
{
    public partial class frmBuscarBoleta : Form
    {
        public frmBuscarBoleta()
        {
            InitializeComponent();
        }
        BoletaBL laBoletaBL = new BoletaBL();
        frmBoleta formBoleta = new frmBoleta();
        private void CargarListaBoletasEmitidas(String Fecha) {
            if (dgvListaBoleta.DataSource == null) {
                dgvListaBoleta.DataSource = laBoletaBL.ObtenerListaBoletasEmitidas(Fecha);
            }
        }
        private void InhabilitarBotonesBoleta() {
            formBoleta.btnNuevo.Enabled = false;
            formBoleta.btnBuscar.Enabled = false;
            formBoleta.btnActualizar.Enabled = true;
            formBoleta.btnCancelar.Enabled = true;
        }
        public void RecuperarListaSeriePorVentaFactura(Int32 IDComprobante, String TipoComprobante)
        {
            frmDesactivarSerie ListaSer = new frmDesactivarSerie();
            ListaSer.CargarSeriesDeComprobanteVenta(IDComprobante, TipoComprobante);
        }

        //--> EVENTOS <--

        private void dgvListaBoleta_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                Int32 nroFila = Int32.Parse(e.RowIndex.ToString());
                formBoleta.MdiParent = this.MdiParent;
                formBoleta.ObtenerTCambio();
                formBoleta.txtNroBoleta.Text = Convert.ToString(dgvListaBoleta["NroBoleta",nroFila].Value);
                formBoleta.lblNroBoleta.Text = Convert.ToString(dgvListaBoleta["NroBoleta", nroFila].Value);
                formBoleta.lblBoletaID.Text = Convert.ToString(dgvListaBoleta["BoletaID", nroFila].Value);
                formBoleta.cmbTipoMoneda.Text = Convert.ToString(dgvListaBoleta["Moneda", nroFila].Value);
                formBoleta.txtTCambioComprob.Text = Convert.ToString(dgvListaBoleta["TCambioBol", nroFila].Value);
                formBoleta.dtpFechaEmision.Value = Convert.ToDateTime(dgvListaBoleta["Fecha Emision", nroFila].Value);
                formBoleta.LlenarComboCliente();
                formBoleta.cmbRazonSocial.SelectedValue = Convert.ToString(dgvListaBoleta["RUC_DNI_Cliente", nroFila].Value);
                formBoleta.LlenarDatosCliente(Convert.ToString(formBoleta.cmbRazonSocial.SelectedValue));
                formBoleta.CargarDetalleBoleta();
                formBoleta.cmbFormaPago.SelectedItem = Convert.ToString(dgvListaBoleta["TCancelacionBol", nroFila].Value);
                formBoleta.txtNroDias.Text = Convert.ToString(dgvListaBoleta["DiasPlasoPago", nroFila].Value);
                formBoleta.txtFechaVence.Text = Convert.ToString(formBoleta.dtpFechaEmision.Value.AddDays(Convert.ToDouble(dgvListaBoleta["DiasPlasoPago", nroFila].Value)));
                formBoleta.txtPago.Text = Convert.ToString(dgvListaBoleta["PagoCon", nroFila].Value);
                formBoleta.gbDatos.Enabled = true;
                InhabilitarBotonesBoleta();
                formBoleta.Show();
                this.Close();
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }

        private void frmBuscarBoleta_Load(object sender, EventArgs e)
        {
            try
            {
                dtpFecha.Text = DateTime.Now.ToShortDateString();
                CargarListaBoletasEmitidas(dtpFecha.Text);
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }

        private void btnMostrarAll_Click(object sender, EventArgs e)
        {
            dgvListaBoleta.DataSource = null;
            CargarListaBoletasEmitidas("0");
        }

        private void btnRegresar_Click(object sender, EventArgs e)
        {
            try
            {
                formBoleta.MdiParent = this.MdiParent;
                formBoleta.Show();
                this.Close();
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }

        private void dtpFecha_ValueChanged(object sender, EventArgs e)
        {
            try
            {
                dgvListaBoleta.DataSource = null;
                CargarListaBoletasEmitidas(dtpFecha.Text);
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }

        private void btnCancelar_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
